#include<bits/stdc++.h>
using namespace std;
int T,n,a[100001],b[100001];
int maxn(int x,int y,int z){
    return x+y+z-min(min(x,y),z);
}
int search_(int x,int y){
    int z=0;
    if(x+1==y){
        if(a[x]==a[y]){
            return a[x];
        }else{
            return 0;
        }
    }
    if(a[x]==a[y]){
        z=a[x];
    }
    return maxn(search_(x,y-1),search_(x+1,y),z);
}
int main(){
    freopen("color.in","r",stdin);
    freopen("color.out","w",stdout);
    scanf("%d",&T);
    while(T--){
        scanf("%d",&n);
        for(int i=1;i<=n;i++){
            scanf("%d",&a[i]);
        }printf("%d\n",search_(1,n));
    }

    return 0;
}
